package org.adream.account.util;

import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.sensorsdata.analytics.javasdk.SensorsAnalytics;

@Component
public class SensorDataUtil {
	
	private SensorDataUtil() {
		
	}
	
	@Value("${sensor.data.logfile}")
	private static String sdLogFile;
	
	private static SensorsAnalytics sa;
	
	private final static Logger logger = Logger.getLogger(SensorDataUtil.class);
	
	static {
		try {
			sa = new SensorsAnalytics(new SensorsAnalytics.ConcurrentLoggingConsumer(sdLogFile));
			//设置定时器，每60s推送一次数据
			new Timer().schedule(new TimerTask() {
				public void run() {
					SensorDataUtil.saFlush();
				}
				
			},60000L,60000L);
		} catch (Exception e) {
			logger.warn("获取神测数据工具初始化错误:(");
		}
	}
	protected static void saFlush() {
		sa.flush();
	}
	
	public static void saProfileSet(String uid, boolean b, String string, String unionid) {
		if(sa==null)
    		return;
    	try {
    		sa.profileSet(uid, b, string, unionid);
		} catch (Exception e) {
			logger.warn(e.getMessage());
		}
	}
	public static void saProfileSet(String uid, boolean b1, String string, boolean b2) {
		if(sa==null)
    		return;
    	try {
    		sa.profileSet(uid, b1, string, b2);
		} catch (Exception e) {
			logger.warn(e.getMessage());
		}
	}
	public static void saProfileSet(String uid, boolean b1,  Map<String,Object> m) {
		if(sa==null)
    		return;
    	try {
    		sa.profileSet(uid, b1, m);
		} catch (Exception e) {
			logger.warn(e.getMessage());
		}
	}
    public static void saTrack(String distinctId, boolean isLoginId, String eventName, Map<String, Object> properties) {
    	if(sa==null)
    		return;
    	try {
    		sa.track(distinctId, isLoginId, eventName, properties);
		} catch (Exception e) {
			logger.warn(e.getMessage());
		}
    }
	public static void saTrackSignUp(String uid, String anonymousId) {
		try {
    		sa.trackSignUp(uid,anonymousId);
		} catch (Exception e) {
			logger.warn(e.getMessage());
		}
		
	}
	public static void saProfileSetOnce(String distinctId, boolean isLoginId, String property, Object value) {
		try {
    		sa.profileSetOnce(distinctId,isLoginId,property,value);
		} catch (Exception e) {
			logger.warn(e.getMessage());
		}
		
	}
}